package com.dinzeer.legendreliclib.lib.key;

import com.dinzeer.legendreliclib.Legendreliclib;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RegisterKeyMappingsEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.lwjgl.glfw.GLFW;


import static com.dinzeer.legendreliclib.Legendreliclib.MODID;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, value = {Dist.CLIENT})
public class LRModKeyMappings {
    public static final KeyMapping TypeMaker = new KeyMapping("key."+MODID+".typemaker", GLFW.GLFW_KEY_Z, "key."+MODID+".neotype") {
        private boolean isDownOld = false;

        @Override
        public void setDown(boolean isDown) {
            super.setDown(isDown);
            // 修复：添加状态检查避免重复发送
            if (isDownOld != isDown && isDown) {
                Legendreliclib.PACKET_HANDLER.sendToServer(new TypeMakerMessage(0, 0));
                isDownOld = isDown;
            } else if (!isDown) {
                isDownOld = false; // 重置状态
            }
        }
    };
    @SubscribeEvent
    public static void registerKeyMappings(RegisterKeyMappingsEvent event) {
        event.register(TypeMaker);
        event.register(SpecialTypeKey1);
        event.register(SpecialTypeKey2);
        event.register(SpecialTypeKey3);
        event.register(SpecialTypeKey4);
        event.register(SpecialTypeKey5);
    }

    @Mod.EventBusSubscriber({Dist.CLIENT})
    public static class KeyEventListener {
        @SubscribeEvent
        public static void onClientTick(TickEvent.ClientTickEvent event) {
            if (event.phase == TickEvent.Phase.END) {
                if (Minecraft.getInstance().screen == null) {
                    TypeMaker.consumeClick();
                    SpecialTypeKey1.consumeClick();
                    SpecialTypeKey2.consumeClick();
                    SpecialTypeKey3.consumeClick();
                    SpecialTypeKey4.consumeClick();
                    SpecialTypeKey5.consumeClick();
                }
            }
        }
    }














    // 五个特殊类型按键
    public static final KeyMapping SpecialTypeKey1 = createSpecialKeyMapping("specialkey1", GLFW.GLFW_KEY_N, 0);
    public static final KeyMapping SpecialTypeKey2 = createSpecialKeyMapping("specialkey2", GLFW.GLFW_KEY_H, 1);
    public static final KeyMapping SpecialTypeKey3 = createSpecialKeyMapping("specialkey3", GLFW.GLFW_KEY_J, 2);
    public static final KeyMapping SpecialTypeKey4 = createSpecialKeyMapping("specialkey4", GLFW.GLFW_KEY_K, 3);
    public static final KeyMapping SpecialTypeKey5 = createSpecialKeyMapping("specialkey5", GLFW.GLFW_KEY_L, 4);


    private static KeyMapping createSpecialKeyMapping(String name, int keyCode, int keyIndex) {
        return new KeyMapping("key." + MODID + "." + name, keyCode, "key." + MODID + ".neotype") {
            private boolean isDownOld = false;

            @Override
            public void setDown(boolean isDown) {
                super.setDown(isDown);
                // 修复：添加状态检查避免重复发送
                if (isDownOld != isDown) {
                    if (isDown) {
                        // 按键按下
                        Legendreliclib.INSTANCE.sendToServer(new SpecialTypeKeyMessage(keyIndex, true));
                    } else {
                        // 按键松开
                        Legendreliclib.INSTANCE.sendToServer(new SpecialTypeKeyMessage(keyIndex, false));
                    }
                    isDownOld = isDown;
                }
            }
        };
    }







}
